import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/screen_util.dart';
import 'package:study_online/entity/Problem.dart';
import 'package:study_online/global/colors.dart';
import 'package:study_online/global/myIcons.dart';
import 'package:study_online/pages/courseStudyPage/views/PublishRemarkPage.dart';
import 'package:study_online/pages/discussPage/views/PublishDiscussPage.dart';
import 'ProblemItem.dart';
import 'package:study_online/routes/CustomRoute.dart';
import 'DiscussTab.dart';
void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      builder: EasyLoading.init(),
      home: DiscussPage(1),
    );
  }
}

class DiscussPage extends StatefulWidget {
  int couId;
  DiscussPage(this.couId);
  @override
  _DiscussPageState createState() => _DiscussPageState();
}

class _DiscussPageState extends State<DiscussPage> {
  List<Tab> _tabs = [
    Tab(
      text: "同学发布",
    ),
    Tab(
      text: "老师发布",
    ),
    Tab(
      text: "我的发布",
    ),
  ];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(
      BoxConstraints(
          maxWidth: MediaQuery.of(context).size.width,
          maxHeight: MediaQuery.of(context).size.height),
      designSize: Size(1080, 2340),
    );
    List<Widget> _tabViews = [
      Container(
        color: Colors.white,
        child:DiscussTab(couId: widget.couId,type: 1,),
      ),
      Container(
        color: Colors.white,
        child:DiscussTab(couId: widget.couId,type: 0,),
      ),
      Container(
        color: Colors.white,
        child:DiscussTab(couId: widget.couId,type: 2,),
      ),
    ];

    return DefaultTabController(
      length: _tabs.length,
      child: Scaffold(
        backgroundColor: Color(0xffFEF6EC),
        body: NestedScrollView(
          headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
            return [
              SliverAppBar(
                pinned: true,
                //title固定在顶部不会滑动出屏幕
                expandedHeight: ScreenUtil().setHeight(480),
                leading: IconButton(
                  icon: Icon(
                    Icons.arrow_back,
                    color: Colors.black45,
                  ),
                  onPressed: () {
                    Navigator.pop(context);
                  },
                ),
                actions: [
                  IconButton(
                    icon: Icon(
                      MyIcons.share,
                      size: ScreenUtil().setSp(55),
                      color: Colors.black45,
                    ),
                    onPressed: () {},
                  ),
                ],
                elevation: 0,
                backgroundColor: Colors.white,
                flexibleSpace: FlexibleSpaceBar(
                  title: Text(
                    "在线交流",
                    style: TextStyle(
                      color: Colors.black,
                      fontFamily: '方正御墨行书',
                    ),
                  ),
                  background: Image.asset(
                    "assets/img/discuss_bg.png",
                    fit: BoxFit.cover,
                    height: ScreenUtil().setHeight(480),
                  ),
                ),
              ),
              SliverPersistentHeader(
                delegate: _SliverAppBarDelegate(
                  TabBar(
                    tabs: _tabs,
                    isScrollable: true,
                    indicatorSize: TabBarIndicatorSize.label,
                    labelColor: Colors.black87,
                    unselectedLabelColor: Colors.grey,
                  ),
                ),
                pinned: true,
              ),
            ];
          },
          body: TabBarView(
            children: _tabViews,
          ),
        ),
        floatingActionButton: InkWell(
          onTap: () {
            Navigator.of(context).push(CustomRoute(PublishDiscussPage(couId:widget.couId)));
          },
          child: Container(
            width: ScreenUtil().setWidth(120),
            height: ScreenUtil().setWidth(120),
            decoration: BoxDecoration(
              color: AppColors.yellow,
              borderRadius:
                  BorderRadius.all(Radius.circular(ScreenUtil().setWidth(60))),
            ),
            child: Center(
              child: Icon(Icons.add, color: Colors.white),
            ),
          ),
        ),
      ),
    );
  }
}

class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
  final TabBar tabBar;

  _SliverAppBarDelegate(this.tabBar);

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    // TODO: implement build
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.only(
            topRight: Radius.circular(16), topLeft: Radius.circular(16)),
        color: Colors.white,
      ),
      child: tabBar,
    );
  }

  @override
  // TODO: implement maxExtent
  double get maxExtent => tabBar.preferredSize.height;

  @override
  // TODO: implement minExtent
  double get minExtent => tabBar.preferredSize.height;

  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
    // TODO: implement shouldRebuild
    return false;
  }
}
